<?php
//--------------------------------------------------------------------------------------
function getFolder($path)
{
	foreach (glob($path) as $filename)
		include $filename;
}

if($_GET['action'] == "login"){
	getFolder("model/*.php");
	$id = $_POST['brokerID'];
	$pass = $_POST['pass'];
	$user = new User();
	if($user->authentication($id, $pass))
	{
		header("Location: BrokerSite.php");
	}
	else{
		header("Location: warningSite.html");
	}

}
//--------------------------------------------------------------------------------------
if ($_GET['action'] == "inputStock")
{
	getFolder("model/*.php");
	$scode= $_POST['scode'];
	$sprice=$_POST['sprice'];
	$sname=$_POST['sname'];
	$stock = new Stock();
	if(!($stock->isAlready($scode)))
	{
		$stock->setStock($scode, $sprice,$sname);
	}
	else{
		echo "Have";
	}
}

if ($_GET['action'] == "showNameStock")
{
	getFolder("model/*.php");
	$code = $_GET['code'];
	$stock = new Stock();
	$stock->findNameStockByCode($code);
}
//--------------------------------------------------------------------------------------
if ($_GET['action'] == "inputBroker")
{
	getFolder("model/*.php");
	$username = $_GET['username'];
	$password = $_GET['password'];
	$broker = new Broker();
	if (!($broker->isAlready($username)))
	{
		$broker->setBroker($username, $password);
		$broker->showTable();
	}
	else
	{
		echo "Have";
	}
}

if ($_GET['action'] == "showTableBroker")
{
	getFolder("model/*.php");
	$broker = new Broker();
	$broker->showTable();
}

if ($_GET['action'] == "deleteBroker")
{
	getFolder("model/*.php");
	$username = $_GET['username'];
	$broker = new Broker();
	$broker->deleteBroker($username);
	$broker->showTable();
}
//--------------------------------------------------------------------------------------
if ($_GET['action'] == "showLiveBoard")
{
	getFolder("model/*.php");
	$liveBoard = new LiveBoard();
	$liveBoard->showLiveBoard();
}


//--------------------------------------------------------------------------------------
if ($_GET['action'] == "showRankUser")
{
	getFolder("model/*.php");
	$number = $_GET['number'];
	$user = new User();
	$user->showRank($number);
}

if ($_GET['action'] == "showUser")
{
	getFolder("model/*.php");
	$user = new User();
	$user->showUser();
}

if ($_GET['action'] == "deleteUser")
{
	getFolder("model/*.php");
	$userid = $_GET['userid'];
	$user = new User();
	$user->deleteUser($userid);
	$user->showUser();
}

if ($_GET['action'] == "showEditUser")
{
	getFolder("model/*.php");
	$userid = $_GET['userid'];
	$user = new User();
	$user->showEditUser($userid);
}

if ($_GET['action'] == "editUser")
{
	getFolder("model/*.php");
	$userid = $_GET['userid'];
	$name = $_GET['name'];
	$clas = $_GET['clas'];
	$id = $_GET['id'];
	$user = new User();
	$user->editUser($userid,$name,$clas,$id);
	$user->showUser();
}

if ($_GET['action'] == "inputUser")
{
	getFolder("model/*.php");
	$name = $_POST['name'];
	$clas = $_POST['clas'];
	$id = $_POST['id'];
	$user = new User();
	$user->setUser($name, $clas, $id);
	$user->showUser();
}

if ($_GET['action'] == "showNameByUserID")
{
	getFolder("model/*.php");
	$userid = $_GET['userid'];
	$user = new User();
	$user->findUserById($userid);
}

//Action sell and buy
if ($_GET['action'] == "checkUserBuy")
{
	getFolder("model/*.php");
	$totalprice = intval(floatval($_GET['price'])) * intval($_GET['amount']);
	$userid = $_GET['userid'];
	$buysell = new BuySell();
	$buysell->checkUserBuy($totalprice, $userid);
}

if ($_GET['action'] == "checkUserSell")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$buysell = new BuySell();
	$buysell->checkUserSell($stockid, $userid, $amount);
}

//Action for LO
if ($_GET['action'] == "buyLO")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$code = $_GET['code'];
	$price = $_GET['price'];
	$buysell = new BuySell();
	$buysell->actionBuyLO($stockid, $userid, $amount, $code, $price);
}

if ($_GET['action'] == "sellLO")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$code = $_GET['code'];
	$price = $_GET['price'];
	$buysell = new BuySell();
	$buysell->actionSellLO($stockid, $userid, $amount, $code, $price);
}

//Action for ATO - ATC
if ($_GET['action'] == "buyAT")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$code = $_GET['code'];
	$price = $_GET['price'];
	$buysell = new BuySell();
	$buysell->actionBuyAT($stockid, $userid, $amount, $code, $price);
}

if ($_GET['action'] == "sellAT")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$code = $_GET['code'];
	$price = $_GET['price'];
	$buysell = new BuySell();
	$buysell->actionSellAT($stockid, $userid, $amount, $code, $price);
}

//Action for MP
if ($_GET['action'] == "buyMP")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$code = $_GET['code'];
	$buysell = new BuySell();
	$buysell->actionBuyMP($stockid, $userid, $amount, $code);
}

if ($_GET['action'] == "sellMP")
{
	getFolder("model/*.php");
	$stockid = $_GET['stockid'];
	$userid = $_GET['userid'];
	$amount = $_GET['amount'];
	$code = $_GET['code'];
	$buysell = new BuySell();
	$buysell->actionSellMP($stockid, $userid, $amount, $code);
}
?>